package com.aaa.service.impl;

import com.aaa.dao.CheckItemDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.CheckItem;
import com.aaa.entity.Dept;
import com.aaa.entity.User;
import com.aaa.service.CheckItemService;
import com.aaa.vo.CheckItemVo;
import com.aaa.vo.DeptVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemDao, CheckItem> implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    //分页查询
    @Override
    public Result<IPage<CheckItem>> findByConditions(Integer currentPage, Integer pageSize, CheckItemVo checkItemVo) {
        IPage<CheckItem> page=new Page<>(currentPage,pageSize);
        QueryWrapper<CheckItem> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(checkItemVo.getCheckItemName())){
            wrapper.like("check_item_name",checkItemVo.getCheckItemName());
        }
        if(StringUtils.hasText(checkItemVo.getKeywords())){
            wrapper.like("keywords",checkItemVo.getKeywords());
        }
        if(StringUtils.hasText(checkItemVo.getUnit())){
            wrapper.like("unit",checkItemVo.getUnit());
        }
        if(Objects.nonNull(checkItemVo.getCheckItemId())){
            wrapper.eq("check_item_id",checkItemVo.getCheckItemId());
        }
        if(Objects.nonNull(checkItemVo.getUnitPrice())){
            wrapper.eq("unit_price",checkItemVo.getUnitPrice());
        }
        if(Objects.nonNull(checkItemVo.getCost())){
            wrapper.eq("cost",checkItemVo.getCost());
        }
        if(Objects.nonNull(checkItemVo.getTypeId())){
            wrapper.eq("type_id",checkItemVo.getTypeId());
        }
        if(Objects.nonNull(checkItemVo.getStatus())){
            wrapper.eq("status",checkItemVo.getStatus());
        }
        if(Objects.nonNull(checkItemVo.getDateRange())&&checkItemVo.getDateRange().length==2){
            wrapper.between("create_time",checkItemVo.getDateRange()[0],checkItemVo.getDateRange()[1]);
        }
        IPage<CheckItem> page1 = checkItemDao.selectPage(page, wrapper);
        return new Result<IPage<CheckItem>>(2000,"查询用户成功",page1);
    }
    //删除
    @Override
    public boolean delById(Long id) {
        int i = checkItemDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }
    //添加修改
    @Override
    public boolean saveAndUpdate(CheckItem checkItem) {
        int i=-1;
        if (checkItem.getCreateTime()==null){
            checkItem.setCreateTime(new Date());
            checkItem.setUpdateTime(new Date());
        }else {
            checkItem.setUpdateTime(new Date());
        }
        if(checkItem.getCheckItemId()==null){
            i=checkItemDao.insert(checkItem);
            System.out.println("====="+checkItem.getStatus());
        }else {
            i=checkItemDao.updateById(checkItem);
        }
        if (i>0){
            return true;
        }
        return false;
    }
}
